package com.swz.dcrm.widget;

import android.content.Context;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.swz.dcrm.R;
import com.swz.dcrm.util.Tool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class NearlySenvenDayView extends LinearLayout {

    private List<TextView> datas;
    private List<TextView> titles;
    private int position=1;

    private com.xh.baselibrary.callback.OnClickListener<Integer> onItemClickListener;
    private TextView lastClick;
    public NearlySenvenDayView(Context context) {
        super(context);
        init();
    }
    public NearlySenvenDayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NearlySenvenDayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        titles=new ArrayList<>();
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        setBackgroundColor(getResources().getColor(R.color.white));
        LinearLayout title=new LinearLayout(getContext());
        LinearLayout.LayoutParams layoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        title.setLayoutParams(layoutParams);
        for(int i=1;i<=7;i++){
            TextView t=new TextView(getContext());
            t.setTextColor(getContext().getResources().getColor(R.color.gary_6f777d));
            t.setTextSize(11);
            t.setGravity(Gravity.CENTER);
            if(i==1){
                t.setText("今天");
            }else if(i==2){
                t.setText("明天");
            }else {
                t.setText(getDayAfter("MM-dd",i-1));
            }
            TextPaint tp = t.getPaint();
            tp.setFakeBoldText(true);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            title.addView(t,lp);
            titles.add(t);
        }
        addView(title);




        datas=new ArrayList<>();
        LinearLayout data=new LinearLayout(getContext());
        LinearLayout.LayoutParams lp1=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lp1.setMargins(0, Tool.dip2px(getContext(),8),0,0);
        data.setLayoutParams(lp1);

        for(int i=1;i<=7;i++){
            TextView t=new TextView(getContext());
            t.setTextColor(getContext().getResources().getColor(R.color.dark_909399));
            t.setTextSize(18);
            t.setGravity(Gravity.CENTER);
            t.setText(0+"");
            TextPaint tp = t.getPaint();
            tp.setFakeBoldText(true);
            datas.add(t);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            int finalI = i;
            t.setOnClickListener(v -> {
                position=finalI;
                TextView tv= (TextView) v;
                if(lastClick!=null){
                    lastClick.setBackground(null);
                    if(Integer.valueOf(lastClick.getText().toString())>0){
                        lastClick.setTextColor(getResources().getColor(R.color.black_2f3234));
                    }else {
                        lastClick.setTextColor(getResources().getColor(R.color.dark_909399));
                    }
                }
                tv.setBackgroundResource(R.drawable.shape_circle_blue);
                tv.setTextColor(getResources().getColor(R.color.white));
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(finalI-1);
                }
                lastClick= tv;
            });
            LinearLayout linearLayout=new LinearLayout(getContext());
            LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(Tool.dip2px(getContext(),28), Tool.dip2px(getContext(),28));
            linearLayout.setGravity(Gravity.CENTER);
            linearLayout.addView(t,lp2);
            data.addView(linearLayout,lp);
        }
        addView(data);
    }

    public void click(){
        datas.get(position-1).performClick();
    }

    public void setOnItemClickListener(com.xh.baselibrary.callback.OnClickListener<Integer> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public void setData(List<String> list){
        for(int i=0;i<list.size();i++){
            if(Integer.valueOf(list.get(i))>0){
                datas.get(i).setTextColor(getContext().getResources().getColor(R.color.black_2f3234));
            }else {
                datas.get(i).setTextColor(getContext().getResources().getColor(R.color.dark_909399));
            }
            datas.get(i).setText(list.get(i));
        }
    }
    private String getDayAfter(String format,int i){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf2 = new SimpleDateFormat(format);
        calendar.add(Calendar.DATE, i);
        return sdf2.format(calendar.getTime());
    }
    public String getCurrentText(){
        return titles.get(position-1).getText().toString();
    }
}
